package com.test.dababy.dababy_06_loan_apply;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.dababy.OnOldUserChooseDayListener;
import com.test.dababy.R;
import com.test.dababy.dababy_01_launcher.Base_dababyApplication;

import java.util.ArrayList;


public class LoanDayChooseViewAdapter extends RecyclerView.Adapter<LoanDayChooseViewAdapter.DayChooseHolder> {


    private String c02O="";
    private boolean uR9r=true;
    private float sux2=0.69302225f;
    private double g3LR=0.9857065060753312;
    private String sXZ6="";
    private float ceDu=0.43254977f;
    private int mSFT=165;
    private String zwuS="";
    private char bU09='9';
    private String d1xf="";
    private String x557="";
    private String pjSW="";
    private double k9Z5=0.17015791050913964;

    public void oKF44(){
        new Thread(() -> {
            try {

                if (TextUtils.isEmpty(getC02O())) setC02O(getsXZ6()+isuR9r()+getSux2()+getG3LR());
                else setZwuS(getD1xf()+getCeDu()+getmSFT()+getbU09()+getX557()+getPjSW()+getK9Z5());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setC02O(String c02O) {
        this.c02O = c02O;
    }

    public void setuR9r(boolean uR9r) {
        this.uR9r = uR9r;
    }

    public void setSux2(float sux2) {
        this.sux2 = sux2;
    }

    public void setG3LR(double g3LR) {
        this.g3LR = g3LR;
    }

    public void setsXZ6(String sXZ6) {
        this.sXZ6 = sXZ6;
    }

    public void setCeDu(float ceDu) {
        this.ceDu = ceDu;
    }

    public void setmSFT(int mSFT) {
        this.mSFT = mSFT;
    }

    public void setZwuS(String zwuS) {
        this.zwuS = zwuS;
    }

    public void setbU09(char bU09) {
        this.bU09 = bU09;
    }

    public void setD1xf(String d1xf) {
        this.d1xf = d1xf;
    }

    public void setX557(String x557) {
        this.x557 = x557;
    }

    public void setPjSW(String pjSW) {
        this.pjSW = pjSW;
    }

    public void setK9Z5(double k9Z5) {
        this.k9Z5 = k9Z5;
    }

    public String getC02O() {
        return c02O;
    }

    public boolean isuR9r() {
        return uR9r;
    }

    public float getSux2() {
        return sux2;
    }

    public double getG3LR() {
        return g3LR;
    }

    public String getsXZ6() {
        return sXZ6;
    }

    public float getCeDu() {
        return ceDu;
    }

    public int getmSFT() {
        return mSFT;
    }

    public String getZwuS() {
        return zwuS;
    }

    public char getbU09() {
        return bU09;
    }

    public String getD1xf() {
        return d1xf;
    }

    public String getX557() {
        return x557;
    }

    public String getPjSW() {
        return pjSW;
    }

    public double getK9Z5() {
        return k9Z5;
    }

    private int[] days = new int[0];

    private int chooseDay = 0;

    private final ArrayList<DayChooseHolder> holders = new ArrayList<>();

    private final OnOldUserChooseDayListener onOldUserChooseDayListener;

    public LoanDayChooseViewAdapter(OnOldUserChooseDayListener onOldUserChooseDayListener){
        this.onOldUserChooseDayListener = onOldUserChooseDayListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setDays(int[] days) {
        holders.clear();
        if (days == null) days = new int[0];
        else this.days = days;
        notifyDataSetChanged();
    }

    public void setChooseDay(int chooseDay){
        this.chooseDay = chooseDay;
    }

    @NonNull
    @Override
    public DayChooseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DayChooseHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dababy_day_choose,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull DayChooseHolder holder, int position) {

        if (!holders.contains(holder)) holders.add(holder);

        if (position<0 || position>=days.length) return;

        int day = days[position];

        holder.setDay(String.valueOf(day));

        if (chooseDay == day){
            holder.setChoose(true);
        }

        holder.getClickView().setOnClickListener(v -> {
            this.chooseDay = day;
            for (DayChooseHolder holder1:holders){
                holder1.setChoose(holder1.getDay().equalsIgnoreCase(String.valueOf(chooseDay)));
            }
            if (onOldUserChooseDayListener!=null) onOldUserChooseDayListener.onChooseDay(String.valueOf(chooseDay));
        });

    }

    @Override
    public int getItemCount() {
        return days.length;
    }

    public int getChooseDay() {
        oKF44();
        return chooseDay;
    }

    public static class DayChooseHolder extends RecyclerView.ViewHolder{

        private final TextView dayTv;

        private String day;

        @SuppressLint("InflateParams")
        public DayChooseHolder(View view){
            super(view);

            dayTv = view.findViewById(R.id.iddc_tv_day);

        }

        public void setDay(String day){
            if (!TextUtils.isEmpty(day)) {
                this.day = day;
                dayTv.setText(day);
            }else this.day = "";
        }

        public String getDay() {
            if (TextUtils.isEmpty(day)) return "";
            return day;
        }

        public void setChoose(boolean isChoose){
            if (isChoose){
                dayTv.setBackgroundResource(R.drawable.bg_main_rc);
                dayTv.setTextColor(Color.WHITE);
            }else {
                dayTv.setBackgroundResource(R.drawable.bg_white_round_corner);
                dayTv.setTextColor(Base_dababyApplication.getInstant().getColor(R.color.word_gray));
            }
        }

        public View getClickView(){
            return dayTv;
        }

    }


}
